package com.culturalCenter.dataCenter.service.impl.user;

import cn.hutool.core.collection.CollUtil;
import com.culturalCenter.dataCenter.Utils.MapperUtil;
import com.culturalCenter.dataCenter.customEntity.responseParam.user.UserLocationListResponse;
import com.culturalCenter.dataCenter.customEntity.user.CustomLocationStatistics;
import com.culturalCenter.dataCenter.customEntity.user.CustomUserLocation;
import com.culturalCenter.dataCenter.entity.user.UserLocation;
import com.culturalCenter.dataCenter.entity.user.UserLocationTypeCount;
import com.culturalCenter.dataCenter.mapper.user.UserLocationCountMapper;
import com.culturalCenter.dataCenter.mapper.user.UserLocationMapper;
import com.culturalCenter.dataCenter.mapper.user.UserLocationPictureMapper;
import com.culturalCenter.dataCenter.mapper.user.UserLocationTypeCountMapper;
import com.culturalCenter.dataCenter.service.user.UserLocationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/****
 * @Author:
 * @Description:UserLocation业务层接口实现类
 * @Date
 *****/
@Service
public class UserLocationServiceImpl implements UserLocationService {

    @Resource
    private UserLocationMapper userLocationMapper;
    @Resource
    private UserLocationCountMapper userLocationCountMapper;
    @Resource
    private UserLocationTypeCountMapper userLocationTypeCountMapper;
    @Resource
    private UserLocationPictureMapper userLocationPictureMapper;
    @Resource
    private MapperUtil mapperUtil;

    @Override
    public PageInfo<UserLocation> findPage(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> userLocationMapper.findPageAll());
    }

    @Override
    public Integer delete(String id) {
        return userLocationMapper.deleteById(id);
    }

    @Override
    public Integer update(UserLocation userLocation) {
        return userLocationMapper.update(userLocation);
    }

    @Override
    public Integer add(UserLocation userLocation) {
        return userLocationMapper.insert(userLocation);
    }

    @Override
    public UserLocation findById(String id) {
        return userLocationMapper.queryById(id);
    }

    @Override
    public List<UserLocation> findAll(UserLocation userLocation) {
        return (List<UserLocation>) userLocationMapper.queryAll(userLocation);
    }

    @Override
    public PageInfo<UserLocationListResponse> findLocationList(String userId, int pageNum, int pageSize) {
        List<UserLocationListResponse> userLocationListResponseList = new ArrayList<>();
        // 获取日期
        List<String> dateList = userLocationMapper.findDateList(userId, (pageNum - 1) * pageSize, pageSize);
        if (CollUtil.isNotEmpty(dateList)) {
            for (String date : dateList) {
                // 用日期获取记录
                List<CustomUserLocation> userLocationList = userLocationMapper.findListByUserAndDate(userId, date);
                userLocationList.forEach(customUserLocation -> {
                    // 获取记录的图片
                    customUserLocation.setPictureList(userLocationPictureMapper.findListByUserAndDateAndCode(userId, customUserLocation.getSiteCode(), date));
                });
                userLocationListResponseList.add(new UserLocationListResponse(date, userLocationList));
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<UserLocationListResponse> pageInfo = new PageInfo<>(userLocationListResponseList);
        PageHelper.clearPage();
        return pageInfo;
    }

    @Override
    public CustomLocationStatistics findByUserId(String userId) {
        CustomLocationStatistics customLocationStatistics = new CustomLocationStatistics();
        UserLocationTypeCount userLocationTypeCount = (UserLocationTypeCount) mapperUtil.getOneResult(userLocationTypeCountMapper, userId);
        if (userLocationTypeCount != null) {
            BeanUtils.copyProperties(userLocationTypeCount, customLocationStatistics);
        }
        customLocationStatistics.setLocationCountList(userLocationCountMapper.queryByUserId(userId));
        return customLocationStatistics;
    }

}
